package com.example.userclient.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.recyclerview.widget.RecyclerView
import com.example.userclient.AddBookExtension
import com.example.userclient.Main.ClientInfoHolder
import com.example.userclient.R
import com.example.userclient.databinding.AddBookItemBinding
import java.sql.Timestamp
import java.text.SimpleDateFormat

object AddBookListAdapter :RecyclerView.Adapter<AddBookListAdapter.ViewHolder>() {

    var dataSet: Array<AddBookExtension>? = null
    val format = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.add_book_item, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        dataSet?.get(position)?.let {
            it.createTime = format.format(Timestamp(it.addBook.createTime))

            holder.changeData(it)
        }
    }



    override fun getItemCount()= dataSet!!.size



    class ViewHolder(private val view: View) : RecyclerView.ViewHolder(view) {

        private val mBinding: AddBookItemBinding? = DataBindingUtil.bind(view)

        fun changeData(addBookExtension: AddBookExtension) {
            if (mBinding != null) {
                mBinding.book = addBookExtension
                Log.i("BuyBookListAdapter", "changeData")
            }
            if (ClientInfoHolder.userBuyBook!!.contains(addBookExtension)){
                mBinding!!.isSellLabel.visibility = View.VISIBLE
            }
        }


    }
}